import api from '../../../utils/api.js'
var WxParse = require('../../../wxParse/wxParse.js');
//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: '',
    imageurl: api.imageurl,
    content: [],
    praisenum: 0,//点赞数
    userId: null,
    cookId: null,
    name: null,
    dianzan: 'false',
    shoucang: 'false',
    review: null,
    scrollTop: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    // console.log('optiondetail', option)
    var that = this;
    var cookId = option.cookId
    var name = option.name
    var userId = app.globalData.userid;
    wx.setNavigationBarTitle({
      title: name,
    })
    that.setData({
      cookId: cookId,
      name: name,
      userId: userId
    })
    // 获取菜谱详情
    api.getCookbookDetail({
      data: {
        id: cookId,
        ou_id: userId,
      },
      success: function (res) {
        // console.log('detail', res.data.result)
        var detail = res.data.result[0];
        var article = detail.content;
        var shoucang = detail.collect;
        var dianzan = detail.praise;
        var praisenum = detail.praiseCount;
        if (shoucang > 0) {
          that.setData({
            shoucang: 'true'
          })
        }
        if (dianzan > 0) {
          that.setData({
            dianzan: 'true'
          })
        }
        WxParse.wxParse('article', 'html', article, that, 5);

        var content = res.data.result[0].content;

        that.setData({
          detail: detail,
          content: content,
          praisenum: praisenum,
        })

      }
    })
  },
  onShow: function () {
    var that = this
    // 获取菜谱评论数据
    api.getReview({
      data: {
        id: that.data.cookId,
      },
      success: function (res) {
        // console.log("评论", res.data.result)
        var review = res.data.result;
        if (review.length <= 0) {
          review = null;
        }

        that.setData({
          review: review
        })

      }
    })
  },
  /**
   * 去发表评论
   */
  toAddReview: function (e) {
    var cookId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../review/add/add?cookId=' + cookId
    })
  },
  // 显示图片
  showImage: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [url],
    })
  },
  /**
   * 回复评论
   */
  toReply: function (e) {
    var that = this
    var reviewId = e.currentTarget.dataset.id;
    var cookId = that.data.cookId;
    wx.navigateTo({
      url: '../review/reply/reply?reviewId=' + reviewId + "&cookId=" + cookId,
    })

  },
  /**
   * 点赞
   */
  addPraise: function () {
    var that = this;
    api.addPraise({
      data: {
        id: that.data.cookId,
        ou_id: that.data.userId
      },
      success: function (res) {
        that.setData({
          dianzan: 'true',
          praisenum: ++that.data.praisenum
        })
        wx.hideLoading()
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  /**
 * 取消点赞
 */
  canclePraise: function () {
    var that = this;
    api.canclePraise({
      data: {
        id: that.data.cookId,
        ou_id: that.data.userId
      },
      success: function (res) {
        that.setData({
          dianzan: 'false',
          praisenum: --that.data.praisenum
        })
        wx.hideLoading()
        wx.showToast({
          title: '取消点赞成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  /**
 * 收藏
 */
  addCollect: function () {
    var that = this;
    api.addCollect({
      data: {
        id: that.data.cookId,
        ou_id: that.data.userId
      },
      success: function (res) {
        that.setData({
          shoucang: 'true'
        })
        wx.hideLoading()
        wx.showToast({
          title: '收藏成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  /**
 * 取消收藏
 */
  cancleCollect: function () {
    var that = this;
    api.cancleCollect({
      data: {
        id: that.data.cookId,
        ou_id: that.data.userId
      },
      success: function (res) {
        that.setData({
          shoucang: 'false',
        })
        wx.hideLoading()
        wx.showToast({
          title: '取消收藏成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  /**
   * 获取评论列表
   */
  getReviewList: function (e) {
    var that = this
    var cookId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../review/list/list?cookId=' + cookId
    })
  },
  /**
     * 分享
     */
  onShareAppMessage: function (e) {
    // console.log('detailshare', e)
    var that = this;
    return {
      title: "小巴依",
      path: '/pages/cookbook/detail/detail?cookId=' + that.data.cookId + '&name=' + that.data.name
    }
  },
  /**
 * 回首页
 */
  gohome: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  scroll: function (e) {
    this.setData({
      scrollTop: e.detail.scrollTop
    })
  },
  totop: function (e) {
    this.setData({
      scrollTop: 0
    })
  },

  /**
  * 联系痛快
  */
  callTongkuai: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: '0532-8898 3947' //仅为示例，并非真实的电话号码
    })
  },
  clickImage: function () {
    var that = this
    var appid = that.data.detail.app_id
    wx.navigateToMiniProgram({
      appId: appid,
      path: 'pages/index/index',
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },
})